package com.tenton.servlet;

import com.tenton.pojo.Orders;
import com.tenton.service.Impl.OrdersServiceImpl;
import com.tenton.service.OrdersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/1/21
 * @Author: Tenton
 * @Description: 查询当前登录用户的所有订单
 */
@WebServlet(name = "ListOrdersServlet",urlPatterns = "/listOrdersServlet")
public class ListOrdersServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        OrdersService ordersService = new OrdersServiceImpl();
        //从session中获取当前登录用户Id
        HttpSession session = request.getSession();
        int userId = (int) session.getAttribute("userId");
        //查询当前登录用户的所有订单
        List<Orders> orders = ordersService.listOrders(userId);
        request.setAttribute("list",orders);
        request.getRequestDispatcher("/orderSuccess.jsp").forward(request,response);
    }
}
